const app = getApp();

Page({
  data: {
    id:'',
    name:'',
    phone:'',
    address:'',
    areaList:{},
    arrIndex:0,
    area_name:'请选择地区',
    picker:0,
    pid:0,
    cid:0,
    aid:0,
    pickerNum:{0:0,1:0,2:0}
  },
  onShareAppMessage() {
    return {
      title: '花礼鲜花玫瑰生日',
      desc: '花礼鲜花玫瑰生日',
      path: 'pages/todos/todos'
    };
  },
  onLoad(r){
    my.setNavigationBar({
      title: '编辑地址',
    });
    var that = this;
    var id = r.id;
    that.setData({
      id:id
    });
    my.getStorage({
      key: 'token',
      success: function(res) {
        my.httpRequest({
          url: app.geturl+'/member-address/info',
          method: 'GET',
          data:{
            token:res.data.token,
            member_address_id:id
          },
          dataType: 'json',
          success: function(ret) {
            if(ret.data.code==0){
              that.setData({
                name:ret.data.data.Obj.consignee,
                phone:ret.data.data.Obj.mobile,
                address:ret.data.data.Obj.address,
                pid:ret.data.data.Obj.province_id,
                cid:ret.data.data.Obj.city_id,
                aid:ret.data.data.Obj.area_id,
                area_name:ret.data.data.Obj.pca_info
              });
            }else{
              app.memberLogin();
            }
          }
        });            
      }
    });
    my.httpRequest({
      url: app.geturl+'/area/list',
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        that.setData({
          objectArray:res.data.data.list
        });
        that.cityList(1);
        that.areaList(36);
      }
    });
    console.info(that.data);
  },
  cityList(id){
    var that = this;
    my.httpRequest({
      url: app.geturl+'/area/list',
      method: 'POST',
      data:{
        area_parent_id:id
      },
      dataType: 'json',
      success: function(res) {
        console.info(res);
        that.setData({
          cityArray:res.data.data.list
        });
        var num = that.data.pickerNum;
        if(num[1]>res.data.data.list.length){
          num[1] = 0;
          num[2] = 0;
          that.setData({
            pickerNum:num
          });
          that.areaList(res.data.data.list[0].area_id);
        }else{
          that.areaList(res.data.data.list[num[1]].area_id);
        }
      }
    });
  },
  areaList(id){
    var that = this;
    my.httpRequest({
      url: app.geturl+'/area/list',
      method: 'POST',
      data:{
        area_parent_id:id
      },
      dataType: 'json',
      success: function(res) {
        that.setData({
          areaArray:res.data.data.list
        })
      }
    });
  },
  subPicker(){
    var that = this;
    var num = that.data.pickerNum;
    var pObj = that.data.objectArray;
    var cObj = that.data.cityArray;
    var aObj = that.data.areaArray;
    that.setData({
      pid:pObj[num[0]].area_id,
      cid:cObj[num[1]].area_id,
      aid:aObj[num[2]].area_id,
      area_name:pObj[num[0]].area_name+cObj[num[1]].area_name+aObj[num[2]].area_name
    });
    that.setData({
      picker:0,
    });
  },
  canclePicker(){
    this.setData({
      picker:0
    });
  },
  onPickerChange(e) {
    var that = this;
    var pObj = that.data.objectArray;
    var pid = e.detail.value[0];
    that.cityList(pObj[pid].area_id);
    that.setData({
      pickerNum:e.detail.value
    });
  },
  checkCity(e) {
    this.setData({
      picker:1,
    });
  },
  blurName(e) {
    this.setData({
      name:e.detail.value
    });
  },
  blurPhone(e) {
    this.setData({
      phone:e.detail.value
    });
  },
  blurAddress(e) {
    this.setData({
      address:e.detail.value
    });
  },
  canclaEdit(){
    my.navigateBack();
  },
  add() {
    app.todos = app.todos.concat([{
      text: this.data.inputValue,
      compeleted: false,
    }]);

    my.navigateBack();
  },
  handleTap(){
    my.navigateTo({ url: '../todos/todos' });
  },
  subAddress(){
    var that = this;
    var name = that.data.name;
    if(!name){
      my.alert({content: '请输入收货人'});
      return;
    }
    var phone = that.data.phone;
    if(!phone){
      my.alert({content: '请输入联系电话'});
      return;
    }
    var address = that.data.address;
    if(!address){
      my.alert({content: '请输入详细地址'});
      return;
    }
    my.getStorage({
      key: 'token',
      success: function(res) {
        my.httpRequest({
          url: app.geturl+'/member-address/update?token='+res.data.token,
          method: 'POST',
          data:{
            member_address_id:that.data.id,
            consignee:name,
            address:address,
            mobile:phone,
            province_id:that.data.pid,
            city_id:that.data.cid,
            area_id:that.data.aid,
          },
          dataType: 'json',
          success: function(res) {
            my.navigateBack({
              delta:1
            })
            console.info(res.data);
          }
        });
      }
    });
  },
});
